package com.captjack.common.util.date;

import java.util.Calendar;

/**
 * @author Capt Jack
 * @date 2018/3/2
 */
public final class DateUtils {

    /**
     * 24小时的毫秒数字
     */
    private static final long TWENTY_FOUR_HOUR_MILL_SECONDS = 24 * 3600 * 1000;

    /**
     * 16小时的毫秒数字
     */
    private static final long SIXTEEN_HOUR_MILL_SECONDS = 16 * 3600 * 1000;

    /**
     * 40小时的毫秒数字
     */
    private static final long FORTY_HOUR_MILL_SECONDS = 40 * 3600 * 1000;

    /**
     * 获取当前时间到当天零点的毫秒数
     *
     * @return 当前时间到当天零点（结束）的毫秒数字，例如当前时间为23:59:59:000，那么会返回1000
     */
    public static long getCurrentToZeroMills() {
        long temp = System.currentTimeMillis() % TWENTY_FOUR_HOUR_MILL_SECONDS;
        return (temp < SIXTEEN_HOUR_MILL_SECONDS) ? SIXTEEN_HOUR_MILL_SECONDS - temp : FORTY_HOUR_MILL_SECONDS - temp;
    }

    /**
     * 获取月份开始utc时间
     *
     * @param shift 偏移量，0：当月、-1：上个月（包含跨年），-2：前两个月，依次类推、1：下个月（包含跨年）
     * @return 当月开始utc时间
     */
    public static long getMonthStartUtc(int shift) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + shift);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }

    /**
     * 获取月份结束utc时间
     *
     * @param shift 偏移量，0：当月、-1：上个月（包含跨年），-2：前两个月，依次类推、1：下个月（包含跨年）
     * @return 当月结束utc时间
     */
    public static long getMonthEndUtc(int shift) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + shift);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTimeInMillis();
    }

    /**
     * 判断当前是否是周末
     *
     * @return 标记
     */
    public boolean judgeTodayIsWeekend() {
        int currentDate = Calendar.getInstance().get(Calendar.DAY_OF_WEEK);
        return currentDate == 1 || currentDate == 7;
    }

    private DateUtils() {

    }

}
